package com.lc.luzhengtang.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.fragment.FragmentFactory2;
import com.lc.luzhengtang.view.ViewFindUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/5.
 */
public class CouponActivity2 extends BaseActivity {
    @BindView(R.id.img_news)
    ImageView imgNews;

    SegmentTabLayout tl_4;

    private String[] mTitles_3 = {"未使用", "已使用", "已过期"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private View mDecorView;
    SegmentTabLayout tabLayout_4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon);
        ButterKnife.bind(this);
        mDecorView = getWindow().getDecorView();
       /* for (String title : mTitles_3) {
            mFragments.add(CouponFragment.getInstance(title));
        }*/
        for (int i = 0; i < mTitles_3.length; i++) {
            mFragments.add(FragmentFactory2.createForExpand(i));
        }
        tabLayout_4 = ViewFindUtils.find(mDecorView, R.id.tl_4);
        tl_3();
    }

    @OnClick(R.id.img_news)
    public void onViewClicked() {
        finish();
    }

    private void tl_3() {
        final ViewPager vp_3 = ViewFindUtils.find(mDecorView, R.id.vp_2);

        vp_3.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        tabLayout_4.setTabData(mTitles_3);
        tabLayout_4.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vp_3.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        vp_3.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabLayout_4.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vp_3.setCurrentItem(0);
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles_3[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
